package com.jiyun.controller;

import com.baomidou.mybatisplus.core.metadata.PageList;
import com.github.pagehelper.PageInfo;
import com.jiyun.feign.FeignCourse;
import com.jiyun.feign.FeignTitle;
import com.jiyun.model.day08.Remark;
import com.jiyun.model.day10.Record;
import com.jiyun.model.day14.Member;
import com.jiyun.service.MemberService;
import com.jiyun.vo.Result;
import com.jiyun.vo.day07.CatalogDto;
import com.jiyun.vo.day08.CourseCto;
import com.jiyun.vo.day08.CourseVo;
import com.jiyun.vo.day10.RecordVo;
import com.jiyun.vo.day10.TopicDto2;
import com.jiyun.vo.day11.RollDto;
import com.jiyun.vo.day11.RollVo;
import com.jiyun.vo.day12.RivalDto2;
import com.jiyun.vo.day12.RivalVo;
import com.jiyun.vo.day14.TeamVo;
import com.jiyun.vo.day15.TraineeDto;
import com.jiyun.vo.day15.TraineeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author yangYingJie
 * @Date 2023/8/22 11：02
 **/
@RestController
@RequestMapping("members")
public class MemberController {

    @Autowired
    private MemberService memberService;
    //杨英杰

    @RequestMapping("login")
    public Result login(Member member) {
        try {
            String token = memberService.login(member);
            if (token != null) {
                return Result.success(token);
            }
            return Result.error();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //展示页面竞赛数量
    @RequestMapping("findByIdRival")
    public Result findByIdRival(HttpServletRequest request) {
        try {
            int a = memberService.findByIdRival(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查看当前登录人的课程数量
    @RequestMapping("findByIdCourse")
    public Result findByIdCourse(HttpServletRequest request) {
        try {
            int a = memberService.findByIdCourse(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findByIdTrainee")
    public Result findByIdTrainee(HttpServletRequest request) {
        try {
            int a = memberService.findByIdTrainee(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<CourseVo> list = memberService.findAll();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //课程点赞
    @RequestMapping("updateDianZan/{id}")
    public Result updateDianZan(@PathVariable("id") Integer id) {
        try {
            memberService.updateDianZan(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //竞赛点赞
    @RequestMapping("dinaZanRoll/{id}")
    public Result dinaZanRoll(@PathVariable("id") Integer id) {
        try {
            memberService.dinaZanRoll(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //竞赛取消点赞
    @RequestMapping("qvXiaoRoll/{id}")
    public Result qvXiaoRoll(@PathVariable("id") Integer id) {
        try {
            memberService.qvXiaoRoll(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }

    @RequestMapping("moRen")
    public Result moRen() {
        try {
            memberService.moRen();
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //最新课程
    @RequestMapping("findReCourse")
    public Result findReCourse() {
        try {
            List<CourseVo> list = memberService.findReCourse();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //最新竞技
    @RequestMapping("findReRoll")
    public Result findReRoll() {
        try {
            List<RollVo> list = memberService.findReRoll();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查询最新培训
    @RequestMapping("findReTrainee")
    public Result findReTrainee() {
        try {
            List<TraineeVo> list = memberService.findReTrainee();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }

    //查询更多课程详情  分页条件查询
    @RequestMapping("findDetailAll")
    public Result findDetailAll(@RequestBody CourseCto courseCto) {
        try {
            PageInfo<CourseVo> page = memberService.findDetailAll(courseCto);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @Autowired
    private FeignTitle feignTitle;

    //查询所有标签信息
    @RequestMapping("findTitle")
    public Result findTitle() {
        try {
            Result all2 = feignTitle.findAll2();
            return all2;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查看单个课程详情
    @RequestMapping("findDetailCourseOne/{id}")
    public Result findDetailCourseOne(@PathVariable("id") Integer id, HttpServletRequest request) {
        try {
            Map<String, Object> map = memberService.findDetailCourseOne(id, request);
            return Result.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //完成课时状态变更
    @RequestMapping("updateCatalogStatus")
    public Result updateCatalogStatus(@RequestBody CatalogDto dto, HttpServletRequest request) {
        try {
            memberService.updateCatalogStatus(dto, request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查看之前先拷贝
    @RequestMapping("addCataloges/{id}")
    public Result addCataloges(@PathVariable("id") String id, HttpServletRequest request) {
        try {
            memberService.addCataloges(id, request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //C端展示竞赛
    @RequestMapping("findRivalAll")
    public Result findRivalAll(@RequestBody RivalDto2 dto) {
        try {
            PageInfo<RivalVo> a = memberService.findRivalAll(dto);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //C端展示单个成员与竞赛的详情
    @RequestMapping("findRivalDetail/{id}")
    public Result findRivalDetail(@PathVariable("id") String id, HttpServletRequest request) {
        try {
            RivalVo rivalVo = memberService.findRivalDetail(id, request);
            return Result.success(rivalVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查询出当前登录人
    @RequestMapping("findByIdMember")
    public Result findByIdMember(HttpServletRequest request) {
        try {
            return memberService.findByIdMember(request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查询所有战队
    @RequestMapping("findAllTeam")
    public Result findAllTeam() {
        try {
            return memberService.findAllTeam();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //报名传递过来一个数据往中间表存入数据
    @RequestMapping("addTeamMember/{id}")
    public Result addTeamMember(@PathVariable("id") String id, HttpServletRequest request) {
        try {
            memberService.addTeamMember(id, request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //C端报名成功之后在RivalMember中间表中添加一条数据
    @RequestMapping("addRivalMember/{id}")
    public Result addRivalMember(@PathVariable("id") String id, HttpServletRequest request) {
        try {
            memberService.addRivalMember(id, request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 根据当前传递过来的id查询它的试卷并查找最大题目数量
     *
     * @return
     */
    @RequestMapping("findCRoll")
    public Result findCRoll(@RequestBody Record record, HttpServletRequest request) {
        try {
            return memberService.findCRoll(record, request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //根据传递过来的试卷id查询出它里面的题目
    @RequestMapping("findCTopic")
    public Result findCTopic(@RequestBody TopicDto2 dto) {
        try {
            return memberService.findCTopic(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //根据传递过来的试卷id查询出它里面的题目与对错
    @RequestMapping("findCTopic2")
    public Result findCTopic2(@RequestBody TopicDto2 dto) {
        try {
            return memberService.findCTopic2(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //点击下一题触发新增选项
    @RequestMapping("/addRecord")
    public Result addRecord(@RequestBody Record record) {
        try {
            return memberService.addRecord(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查询当前登录人的历史记录
    @RequestMapping("findRecord")
    public Result findRecord(@RequestBody TopicDto2 dto) {
        try {
            List<Record> list = memberService.findRecord(dto);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //查分
    @RequestMapping("findFen")
    public Result findFen(@RequestBody TopicDto2 dto) {
        try {
            Integer a = memberService.findFen(dto);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //培训列表展示
    @RequestMapping("findCTrainee")
    public Result findCTrainee(@RequestBody TraineeDto dto) {
        try {
            return memberService.findCTrainee(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 当前登录人报名培训
     */
    @RequestMapping("addTraineeMember/{id}")
    public Result addTraineeMember(@PathVariable("id") String id) {
        try {
            memberService.addTraineeMember(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 查询单个培训详情，
     * 当前登录人是否报名
     */
    @RequestMapping("findTraineeById/{id}")
    public Result findTrianeeById(@PathVariable("id") String id) {
        try {
            TraineeVo traineeVo = memberService.findTrianeeById(id);
            return Result.success(traineeVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 当前登录人的Id  查询是否做过课程
     * 如果做过题就查询到所有课程
     *
     * @param id
     * @return
     */
    @RequestMapping("findByIdCataloges/{id}")
    public Result findByIdCataloges(@PathVariable("id") Integer id) {
        try {
            List<CourseVo> list = memberService.findByIdCataloges(id);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 查询当前培训的试卷Roll
     *
     * @param id
     * @return
     */
    @RequestMapping("findTraineeRoll/{id}")
    public Result findTraineeRoll(@PathVariable("id") Integer id) {
        try {
            List<RollVo> list = memberService.findTraineeRoll(id);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 分页展示试卷信息
     * @param dto
     * @return
     */
    @RequestMapping("findShowRoll")
    public Result findShowRoll(@RequestBody RollDto dto){
        try {
            PageInfo<RollVo> page = memberService.findShowRoll(dto);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //个人中心中心展示竞赛
    @RequestMapping("memberRival")
    public Result memberRival(@RequestBody RivalDto2 dto){
        try {
            List<RivalVo> list =  memberService.memberRival(dto);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //查询当前登录人的战队
    @RequestMapping("findByIdTeam/{id}")
    public Result findByIdTeam(@PathVariable("id")Integer id){
        try {
            TeamVo teamVo = memberService.findByIdTeam(id);
            return Result.success(teamVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //-- 个人中心当前登录人的历史记录
    @RequestMapping("findByIdRecord/{id}")
    public Result findByIdRecord(@PathVariable("id")Integer id){
        try {
            List<RecordVo> list = memberService.findByIdRecord(id);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //当前登录人测试数量
    @RequestMapping("findRollNum")
    public Result findRollNum(HttpServletRequest request){
        try {
            Integer a = memberService.findRollNum(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }
    //MongoDB
    @Autowired
    FeignCourse feignCourse;
    @RequestMapping("findByIdRemark/{id}")
    public Result findByIdRemark(@PathVariable("id") Integer id){
        try {
            List<Remark> byId = feignCourse.findById(id);
            return Result.success(byId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("addRemark")
    public Result addRemark(@RequestBody Remark remark){
        try {
            feignCourse.addRemark(remark);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("deleteRemark/{id}")
    public Result deleteRemark(@PathVariable("id") String id){
        try {
            feignCourse.deleteRemark(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }
}
